package com.yu.task.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.yu.task.globalResult.MsgType;
import com.yu.task.globalResult.R;
import com.yu.task.globalResult.StatusType;
import com.yu.task.pojo.User;
import com.yu.task.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody User user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int i = userService.register(user);
        if (i == 1) {
            return R.success();
        } else if (i == 2) {
            return R.failure(StatusType.ERROR, "电话号码重复");
        }
        return R.failure();
    }


    /**
     * 用户登录 tel+pwd
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody User user) {
        R mp = userService.login(user);
        return mp;
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */

    @PostMapping("/updateInfo")
    @ResponseBody
    public R updateInfo(@RequestBody User user) {

        int  i = userService.updateInfo(user);
        if(i == 1){
            return R.success();
        }
        return R.failure(StatusType.ERROR, MsgType.UNKNOWN_ERROR);
    }

    @PostMapping("/delete")
    @ResponseBody
    public R delete(@RequestParam Long id) {

        int  i = userService.delete(id);
        if(i == 1){
            return R.success();
        }
        return R.failure(StatusType.ERROR, MsgType.UNKNOWN_ERROR);
    }

    /**
     * 查询用户列表分页
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/selectList")
    public R selectList(@RequestParam int page,int size) {

        R r = userService.selectList(page,size);
        return r;
    }

    @GetMapping("/loginOut")
    public R loginOut() {
        StpUtil.logout(StpUtil.getLoginId());

        return R.success("退出");
    }

    @GetMapping("/getUserInfoById")
    public R getUserInfoById(@RequestParam Long uid) {
        return  userService.getInfoById(uid);
    }
}
